$(function () {
    $('#deviceCode').focus();
    //重置表单
    $("#searchReset").on("click",function(){
        $("#searchForm")[0].reset();
    });
    //渲染表格
    var columns = calcList.columnsInit();
    var table = calcList.gridInit(columns);
    var datas = calcList.dataInit();
    $("#calcTable").bootstrapTable("append",datas);
    //点击删除按钮信息弹出框
    $("#del-btn").click(function(){
        var len = $("#calcTable input:checked").length;
        if (len == 0) {
            //未选中数据提示框
            layer.msg('请选择数据',  {time:1000});
        } else {
            layer.confirm('确定要删除数据吗？',
                {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {
                    layer.close(index);
                });
        }

    });
    //新建弹框
    $("#add-btn").on('click',function () {
        layer.open({
            move: false,
            type: 2,
            title: '新建',
            area: ['800px', '500px'],
            content: 'j_forward_calc_add.html',
            btn: ['保存','取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var form= openWin.submitForm();
                if(!form){
                    layer.close(index);
                }
            }, cancel: function (index) {
                layer.close(index);
            }
        });
    })
})
;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var calcList = {
        columnsInit:function(){
            return  [{
                field: 'od',
                title: '序号',
                width:'2%',
                align:'center'
            }, {
                field: 'stable',
                title: '源表格',
                sortable : true
            }, {
                field: 'scode',
                title: '源编码'
            },{
                field: 'ttable',
                title: '目标表格',
            }, {
                field: 'tcode',
                title: '目标编码'
            }, {
                field: 'rowSort',
                title: '排序号'
            }, {
                field: 'remarks',
                title: '备注'
            }, {
                field: 'status',
                title: '状态',
                formatter: function (value, row, index) {
                    if(value == '启用'){
                        return [
                            '<span class="sys-type-bg pro-status-green">启用</span>'
                        ];
                    }
                    return [
                        '<span class="sys-type-bg pro-status-red">停用</span>'
                    ];

                }

            }, {
                field: 'optionEdit',
                title: '编辑',
                width: 60,
                align: 'center',
                formatter: function (value, row, index) {
                    return [
                        '<a class="opt-icon" href="javascript:void(0)" title="编辑" onclick="calcList.toEditPage()">',
                        '<i class="glyphicon glyphicon-pencil"></i>',
                        '</a>  '
                    ].join('');
                }
            }, {
                field: 'optionDel',
                checkbox: true
            }]
        },
        gridInit:function(columns){
            return $("#calcTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                pageNumber: 1,
                pageSize: 10,
                pageList: [
                    10, 30, 50, 100, 200
                ],
                onPostBody:function () {
                    $(".opt-dropdown").each(function(index,data){
                        var _this = this;
                        $(this).hover(function(){
                            $(_this).find(".opt-dropdown-menu").show();
                        },function(){
                            $(_this).find(".opt-dropdown-menu").hide();
                        })
                    });
                }
            })

        },
        dataInit:function(){
            return  [{
                od: 1,
                stable: 'iot_base',
                scode: 'TRQ0001',
                ttable:'iot_day',
                tcode:'TRQ0001',
                rowSort:'12',
                remarks:'备注1',
                status:'停用'
            },{
                od: 2,
                stable: 'iot_base',
                scode: 'TRQ0001',
                ttable:'iot_day',
                tcode:'TRQ0001',
                rowSort:'12',
                remarks:'备注1',
                status:'启用'
            }]
        },
        toEditPage:function () {
            layer.open({
                move: false,
                type: 2,
                title: '编辑',
                area: ['800px', '500px'],
                content: 'j_forward_calc_edit.html',
                btn: ['保存','取消'],
                btnAlign: 'c',
                yes: function (index, layero) {
                    layer.close(index);
                }, cancel: function (index) {
                    layer.close(index);
                }
            });
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = calcList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return calcList;});
    } else {
        !('plugin' in _global) && (_global.calcList = calcList);
    }
}());